package  org.cocos2dx.javascript.Advert.gdt;

import android.app.Activity;
import android.util.Log;

import com.qq.e.ads.rewardvideo.RewardVideoAD;
import com.qq.e.ads.rewardvideo.RewardVideoADListener;
import com.qq.e.comm.util.AdError;
import org.cocos2dx.javascript.Advert.core.AdBase;
import org.cocos2dx.javascript.Advert.core.AdSdkBridge;
import org.cocos2dx.javascript.NativeMgr;
import org.json.JSONObject;

import java.util.Map;

public class GDTRewardVideoAd extends AdBase implements RewardVideoADListener {

    private RewardVideoAD mRewardVideoAD;
    private boolean mIsRewardValid;

    @Override
    public void loadAd(Activity activity, boolean show) {
        mActivity = activity;
        mShow = show;
        if (mRewardVideoAD == null) {
            mRewardVideoAD = new RewardVideoAD(mActivity, GDTConstants.RewardVideoPosID, this);
        }
        mRewardVideoAD.loadAD();
    }

    public void showAd(Activity activity) {
        if (!mLoaded) {
            loadAd(activity, true);
            return;
        }
        mLoaded = false;
        mIsRewardValid = false;
        mRewardVideoAD.showAD();
    }

    @Override
    public void onADLoad() {
        mLoaded = true;
        if (mShow) {
            showAd(mActivity);
        }
    }

    @Override
    public void onVideoCached() {

    }

    @Override
    public void onADShow() {

    }

    @Override
    public void onADExpose() {

    }

    @Override
    public void onReward(Map<String, Object> map) {
        mIsRewardValid = true;
    }

    @Override
    public void onADClick() {

    }

    @Override
    public void onVideoComplete() {

    }

    @Override
    public void onADClose() {
     
        try {
            JSONObject mJsonobjData = new JSONObject();
            mJsonobjData.put("mIsRewardValid", mIsRewardValid);
            NativeMgr.OnCallBackToJs("onRewardAdCallback", mJsonobjData.toString());
        } catch (Exception e) {
            Log.e("GDTRewardVideoAd", "onADClose: ", e);
        }
        
    

    }

    @Override
    public void onError(AdError adError) {
        Log.d("gdt GDTRewardVideoAd", adError.getErrorCode() + " ?onError " + adError.getErrorMsg());
        try {
            JSONObject mJsonobjData = new JSONObject();
            mJsonobjData.put("mIsRewardValid", mIsRewardValid);
            NativeMgr.OnCallBackToJs("onRewardAdCallbackError", adError.getErrorMsg());
        } catch (Exception e) {
            Log.e("GDTRewardVideoAd", "onError: ", e);
        }
        
      
    }
}
